package com.litongjava.ai.server.padddle.ocr.controller;

import com.litongjava.ai.server.padddle.ocr.common.RestResponse;
import com.litongjava.ai.server.padddle.ocr.service.OcrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;

@RestController
@RequestMapping("/api/ocr")
public class OcrController {

    @Autowired
    private OcrService ocrService;

    /**
     * 通过上传文件识别图片文字
     * 
     * @param file 上传的图片文件
     * @return 识别结果
     */
    @PostMapping("/recognize")
    public RestResponse<String> recognizeByFile(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return RestResponse.fail("请选择文件");
            }
            String result = ocrService.recognizeByFile(file);
            return RestResponse.ok(result);
        } catch (Exception e) {
            return RestResponse.fail(e.getMessage());
        }
    }
}